// 初始化AV
import touch from 'utils/touch.js'//新加

// require('utils/init.js');
// var Bmob = require('utils/bmob.js');

App({
	Domain: 'https://sc.tangmu.xin/addons/litestore/api.',
	api_url: '',
	/* 设置api地址 */
	setApiUrl: function () {
	  this.api_url = this.Domain + '';
	},
	onLaunch: function () {
   

    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          console.log("app: " + "用户已经授权")
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo
              console.log(this.globalData.userInfo)
              wx.setStorageSync('userInfo', this.globalData.userInfo);
              this.globalData.hasUserInfo = true
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            },
            fail: (res) => {
              console.log("app: " + "获取用户信息失败")
            }
          })
        } else {
          console.log("app: " + "用户暂时未授权")
        }
      }
    })


		var that = this;
		let islogin=wx.getStorageSync('access_token')
		// if(islogin){
		// 	wx.switchTab({
		// 		url: '../../shop/index/index'
		// 	});
		// }else{
		// 	that.isreg()
		// }
		
		
		// 设备信息
		wx.getSystemInfo({
			success: function(res) {
				that.screenWidth = res.windowWidth;
				that.screenHeight = res.windowHeight;
				that.pixelRatio = res.pixelRatio;
			}
		});
	},
	auth: function () {
		  
		// 管理员认证
		if (!wx.getStorageSync('isAdmin')) {
			wx.switchTab({
				url: '../../shop/index/index'
			});
		}	
	},
	//判断是否登陆
	isreg() {
		var that = this;
		wx.login({
			success: function(res) {
				console.log(res)
				wx.request({
					url: 'https://sc.tangmu.xin/addons/litestore/api.user/checkUserReg',
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					dataType: 'json',
					data: {
						code: res.code
					},
					success: function(lres) {
						console.log(lres.data)
						wx.setStorageSync('session_key', lres.data.data.session_key);
						if (lres.data.ret == 200) {
              wx.setStorageSync('openid', lres.data.data.open_id);
							wx.setStorageSync('access_token', lres.data.data.access_token.access_token);
							wx.switchTab({
								url: '../../shop/index/index',
							})
	
						} else if (lres.data.ret == 201) {
              wx.setStorageSync('openid', lres.data.data.openid);
							that.login(lres.data.data.openid, lres.data.data.session_key)
						}else{
						console.log('失败')
					}
	
					}
				})
			},
		})
	
	},
	//登录
	login(openid, session_key) {
		wx.request({
			url: 'https://sc.tangmu.xin/addons/litestore/api.user/userReg',
			method: 'POST',
			header: {
				'content-type': 'application/x-www-form-urlencoded'
			},
			dataType: 'json',
			data: {
        openid: openid,
				session_key: session_key,
        nickname: wx.getStorageSync('userInfo.nickName'),
        avatar: wx.getStorageSync('userInfo.avatar'),
        
			},
			success: function(lres) {
        console.log(lres)
        if (lres.data.ret==200){

          wx.setStorageSync('access_token', lres.data.data.access_token);
          wx.showToast({
            title: '登陆成功',
            icon: 'success',
            duration: 2000
          })

          // 跳转首页
          setTimeout(function () {
            wx.switchTab({
              url: '../../shop/index/index',
            })
          }, 2500)
        }


        
	
	
			}
		})
	},
	 globalData: {
	    userInfo: null
	  },
	  url: {
	    requestUrl: "https://sc.tangmu.xin/addons/litestore/api." ,// 网络接口全局变量
		requestUrlcontent: "https://sc.tangmu.xin/"
	  },
	 header: {'content-type': 'application/x-www-form-urlencoded',"token":wx.getStorageSync('access_token')},
     touch: new touch()//新加
  
})
